home *** CD-ROM | disk | FTP | other *** search
- /* ciscotalk.c
- *
- * portable across unix/ultrix & vms systems
- * first version (90/02/07) by Daniel Karrenberg (dfk@cwi.nl)
- * this version (91/07/08) by Davide Salomoni (salomoni@cnaf.infn.it)
- * to be linked on VMS systems with the appropriate socket library and
- * with a version of the getopt() function.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <netdb.h>
-
- #ifdef VAXC
- #include <unixio.h>
- #include <types.h>
- #include <socket.h>
- #include <in.h>
- #else
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #endif
-
- #include "ciscotalk.h"
-
- extern int getopt(int, char **, char *);
- static char *iogets(char *string,int n,int descr);
- static readcmd(int s);
-
- char host[255] = SERVER;
- char lbuf[80];
- char verbose = 0;
-
- main(int argc, char **argv)
- {
-
- extern int optind;
- extern char *optarg;
- int i,s;
- char c;
- static struct sockaddr_in sin;
- struct hostent *hp,hs;
- struct servent *sp;
-
- while ((i=getopt(argc,argv,"vh:"))!=EOF) {
- switch(i) {
- case 'h':
- strncpy(host,optarg,sizeof(host));
- break;
- case 'v':
- verbose++;
- break;
- case '?':
- fprintf(stderr,"usage: ciscotalk [-vh host]\n");
- exit(1);
- }
- }
-
- if ((hp=gethostbyname(host)) == NULL) {
- fprintf(stderr, "ciscotalk: %s unknown\n", host);
- exit(1);
- }
- hs = *hp;
- /*
- * hp is a pointer to a static area that could be overwritten by
- * other processes or by a subsequent call to a function that uses
- * the same area; it's therefore necessary to copy the data to a safe
- * place.
- */
-
- if ((s=socket(hs.h_addrtype, SOCK_STREAM, 0)) <0) {
- perror("ciscotalk: cannot create socket");
- exit(1);
- }
-
- #ifdef VAXC
- /*
- * the vms/ultrix connection does not support the getservbyname() function,
- * so we define the port statically converting it first to network byte order.
- */
- sin.sin_port = htons(23);
- #else
- if ((sp=getservbyname("telnet", "tcp")) == NULL)
- {
- fprintf(stderr, "ciscotalk: can't find service\n");
- exit(1);
- }
- sin.sin_port = sp->s_port;
- #endif
-
- sin.sin_family = hs.h_addrtype;
- sin.sin_addr = * ((struct in_addr *) hs.h_addr);
-
- if (verbose)
- fprintf(stderr, "connecting to %s ...\n\n", hp->h_name);
-
- if (connect(s, &sin, sizeof (sin))) {
- perror("ciscotalk: connect");
- exit(1);
- }
-
- sleep(1);
- write(s, "\r\n", 2);
- readcmd(s);
- while (read(0,&c,1) ==1) {
- if (c=='\n') {
- write(s,"\r\n",2);
- readcmd(s);
- }
- else
- write(s, &c, 1);
- }
- exit(0);
-
- }
-
-
- /*
- * read a line from the socket using the low-level i/o, since the vms/ultrix
- * connection does not support standard i/o with sockets.
- */
- static readcmd(int s)
- {
- while (iogets(lbuf,sizeof(lbuf)-1,s)) {
- fputs(lbuf,stdout);
- if (strstr(lbuf,PROMPT1) ||
- strstr(lbuf,PROMPT2) ||
- strstr(lbuf,PROMPT3)) {
- fflush(stdout);
- return;
- }
- }
- fflush(stdout);
- }
-
-
- /* iogets() works like fgets() but uses low-level i/o functions. */
- static char *iogets(char *string,int n,int descr)
- {
- char ch;
- int i;
-
- if (!n) return((string=NULL));
- i=0;
- while (read(descr,&ch,1)==1) {
- string[i]=ch;
- if (++i==n || ch=='\n') {
- string[i]='\0';
- return(string);
- }
- }
- return(NULL);
- }
-